package com.toncen.samepms.job;

import java.util.Vector;

import org.quartz.JobExecutionContext;

import com.toncen.samepms.common.AbstractJob;
import com.toncen.samepms.system.dao.ConfigurationDao;
import com.toncen.samepms.system.dao.LoginLogDao;
import com.toncen.samepms.system.domain.LoginLog;

/**
 * 描述：登录日志任务
 * 
 * @author tch
 * 
 */
public class LoginLogJob extends AbstractJob {
	private volatile static Vector<LoginLog> loginLogs = new Vector<LoginLog>(5);

	/**
	 * @param loginLog
	 * @return
	 */
	public static boolean addLoginLog(LoginLog loginLog) {
		String enableLoginLog = "2";
		try {
			enableLoginLog = new ConfigurationDao().getValueByName("enable_login_log");
		} catch (Exception e) {
			e.printStackTrace();
		}
		if ("1".equalsIgnoreCase(enableLoginLog)) {
			return loginLogs.add(loginLog);
		} else {
			return false;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.toncen.samepms.common.AbstractJob#run(org.quartz.JobExecutionContext)
	 */
	protected void run(JobExecutionContext context) {
		LoginLogDao loginLogDao = new LoginLogDao();
		try {
			LoginLog loginLog;
			while (loginLogs != null && loginLogs.size() >= 1) {// 
				loginLog = loginLogs.get(0);
				loginLogDao.add(loginLog);
				loginLogs.remove(0);
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
	}

}
